package com.example.ecas.service.impl;

import com.example.ecas.common.CommonResponse;
import com.example.ecas.persistence.*;
import com.example.ecas.pojo.*;
import com.example.ecas.persistence.CaseMapper;
import com.example.ecas.persistence.PatientMapper;
import com.example.ecas.persistence.WcRpatMapper;
import com.example.ecas.pojo.Case;
import com.example.ecas.pojo.Patient;
import com.example.ecas.pojo.TinyCase;
import com.example.ecas.pojo.WcRpat;
import com.example.ecas.service.AbstractsService;
import com.example.ecas.service.CaseService;
import com.example.ecas.service.PatientService;
import com.example.ecas.service.WordCloudService;
import org.python.antlr.ast.Str;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class PatientServiceImpl implements PatientService {

    @Autowired
    PatientMapper patientMapper;

    @Autowired
    CaseMapper caseMapper;

    @Autowired
    WordCloudService wordCloudService;

    @Autowired
    AbstractsService abstractsService;

    @Autowired
    CaseService caseService;

    @Autowired
    WcRpatMapper wcRpatMapper;

    @Autowired
    DisRcaseMapper disRcaseMapper;

    @Autowired
    DisRtagMapper disRtagMapper;

    @Autowired
    TagMapper tagMapper;

    // 最多转换三个
    private List<TinyCase> toTinyCases(List<Case> cases) {
        List<TinyCase> tinyCases = new ArrayList<>();
        int i = 0;
        for (Case aCase : cases) {
            TinyCase tinyCase = new TinyCase();
            tinyCase.setId(aCase.getId());
            tinyCase.setTime(aCase.getTime());
            tinyCase.setContent(aCase.getMain());
            tinyCases.add(tinyCase);
            if (i >= 2) {
                break;
            }
            i++;
        }
        return tinyCases;
    }

    // 获取患者画像,返回的数据的列表list
    // list[0]为patient对象, list[1]为tags
    // list[2]为currentCase, list[3]为tinyCase列表
    // list[4]为摘要, list[5]为base64患者词云
    @Override
    public List<Object> getPatImage(int id) {
        List<Object> result = new ArrayList<>();
        Patient patient = patientMapper.getPatient(id);
        if (patient == null) {
            return null;
        }
        result.add(patient);
        // 获取tags
        List<String> tags = new ArrayList<>();

        List<Tag> tagList = this.getPatTags(id);

        String wordCloud = "";

        for (Tag tag: tagList) {
            String strTag = tag.getContent();
            tags.add(strTag);
            wordCloud  = wordCloud + strTag + " ";
        }

        System.out.println(wordCloud);

        result.add(tags);
        List<Case> cases = caseMapper.getPatientCases(id);
        List<Case> _cases = caseService.caseRecommend(id);
        result.add(cases.isEmpty() ? null : cases.get(0));
        result.add(this.toTinyCases(_cases));

        // 获取摘要
        result.add(abstractsService.getCaseAbstracts(cases.get(0).getId()));
//        result.add("这是摘要");
        WcRpat wcRpat = wcRpatMapper.getWcRpat(id);
        String wcB64 = wcRpat!=null ? wcRpat.getData() : "";
        try {
            if (wcB64.isEmpty()) {
                wcB64 = wordCloudService.getWordCloudB64Random(wordCloud.trim());
                wcRpat = new WcRpat();
                wcRpat.setPatId(id);
                wcRpat.setData(wcB64);
                wcRpatMapper.addWcRpat(wcRpat);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.add(wcB64);
        return result;
    }

    @Override
    public List<Tag> getPatTags(int patientId) {
        Case _case = caseMapper.selectById(patientId);
        // case ---> dis
        List<DisRcase> disRcaseList = disRcaseMapper.getDisRcaseByCase(patientId);
        if (disRcaseList == null || disRcaseList.size() < 1) {
            return null;
        } else {
            List<Integer> disRtags = new ArrayList<>();
            for (DisRcase disRcase: disRcaseList) {
                // dis ---> tags
                List<DisRtag> disRtagList = disRtagMapper.getDisRtagByDis(disRcase.getDisId());
                List<Integer> tagIdList = new ArrayList<>();
                for (DisRtag disRtag: disRtagList) {
                    int tagId = disRtag.getTagId();
                    tagIdList.add(tagId);
                }
                disRtags = Stream.concat(disRtags.stream(), tagIdList.stream())
                        .distinct()
                        .collect(Collectors.toList());
            }
            if(disRtags.size() < 1) {
                return null;
            } else {
                List<Tag> resultList = new ArrayList<>();
                List<Tag> tagList = new ArrayList<>();
                for(Integer integer: disRtags) {
                    Tag tag = tagMapper.getTag(integer);
                    tagList.add(tag);
                }
                for (Tag tag: tagList) {
                    String main = _case.getMain();
                    String pre = _case.getPresents();
                    if (main.contains(tag.getContent()) || pre.contains(tag.getContent())) {
                        resultList.add(tag);
                    }
                }
                return resultList;
            }
        }
    }
}
